package cn.falcon.fmobi.console.controller;

import cn.falcon.fmobi.biz.common.Constant;
import cn.falcon.fmobi.biz.exception.AdvertiserValidateException;
import cn.falcon.fmobi.biz.service.*;
import cn.falcon.fmobi.console.common.RestfulJsonModelAndView;
import cn.falcon.fmobi.console.common.Result;
import cn.falcon.fmobi.console.constant.UrlConstant;
import cn.falcon.fmobi.console.constant.UserRolePermission;
import cn.falcon.fmobi.console.exception.CallBackException;
import cn.falcon.fmobi.console.exception.ParamValidateException;
import cn.falcon.fmobi.dao.model.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * User: shirongkang
 * Date: 2017/9/22
 * Time: 10:58
 * Desc:选择菜单
 */
@Controller
@RequestMapping(UrlConstant.COMMON_URL)
public class CommonCtrl extends BaseCtrl {

    @Autowired
    private CommonService commonService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private PromotionPlanService promotionPlanService;

    /**
     * 地域定向菜单
     * @return
     */
    @RequestMapping(value = "regionStruct",method = RequestMethod.GET)
    public ModelAndView getRegionStruct(int type){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getRegionStruct(type));
    }
    /**
     * 人口属性菜单
     * @return
     */
    @RequestMapping(value = "personals",method = RequestMethod.GET)
    public ModelAndView getPersonalStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getPersonalStruct());
    }
    /**
     * 商业兴趣菜单
     * @return
     */
    @RequestMapping(value = "businessInterestStruct",method = RequestMethod.GET)
    public ModelAndView getBusinessInterestStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getBusinessInterestStruct());
    }
    /**
     * 移动设备菜单
     * @return
     */
    @RequestMapping(value = "mobileDeviceStruct",method = RequestMethod.GET)
    public ModelAndView getMobileDeviceStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getMobileDeviceStruct());
    }
    /**
     * 策略状态菜单
     * @return
     */
    @RequestMapping(value = "strategyStatusStruct",method = RequestMethod.GET)
    public ModelAndView getStrategyStatusStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getStrategyStatusStruct());
    }
    /**
     * app行为菜单
     * @return
     */
    @RequestMapping(value = "appStruct",method = RequestMethod.GET)
    public ModelAndView getAppStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getAppStruct());
    }
    /**
     *自定义人群菜单
     * @return
     */
    @RequestMapping(value = "customAudienceStruct",method = RequestMethod.GET)
    public ModelAndView getCustomAudienceStruct(){
        // 权限筛选
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getCustomAudienceStruct(this.getActorId(UserRolePermission.USER_ROLE_ADVERTISER),this.getActorId(UserRolePermission.USER_ROLE_AGENT)));
    }
    /**
     *定向包列表菜单
     * @return
     */
    @RequestMapping(value = "targetPackageStruct",method = RequestMethod.GET)
    public ModelAndView getTargetPackageStruct(Integer campaignId){
        //权限筛选
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getTargetPackageList(this.getActorId(UserRolePermission.USER_ROLE_ADVERTISER),
            this.getActorId(UserRolePermission.USER_ROLE_AGENT),campaignId));
    }

    /**
     *媒体列表菜单
     * @return
     */
    @RequestMapping(value = "mediasStruct",method = RequestMethod.GET)
    public ModelAndView getMediasStruct(){
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getMediasStruct(Constant.PLATFORM_ID_TENCENT));
    }

    @RequestMapping(value = "customer/industry", method = RequestMethod.GET)
    public ModelAndView getAdvertiserIndustryList() {
        return RestfulJsonModelAndView.buildJsonModelAndView(commonService.getAdvertiserIndustryList());
    }

    /**
     * 上传图片
     *
     * @param imageBase64Str 图片Base64字符串
     * @param width          图片宽度
     * @param height         图片高度
     * @param campaignId    订单ID
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload/image", method = RequestMethod.POST)
    public ModelAndView uploadImage(String imageBase64Str, Integer width, Integer height,Integer campaignId) throws IOException {
        Map<String, Object> result = commonService.uploadImage(imageBase64Str, width, height, campaignId);
        return RestfulJsonModelAndView.buildJsonModelAndView(result);
    }

    /**
     * 广告主下的平台列表接口
     *
     * @param advertiserId 广告主id
     * @param session
     * @return
     * @throws ParamValidateException
     */
    @RequestMapping(value = "platformList", method = RequestMethod.GET)
    public ModelAndView getPlatformListByAdvertiserId(@RequestParam(required = false) Integer advertiserId,
        HttpSession session)
        throws ParamValidateException {
        // 获取权限
        Integer advId = getLoginUser(session) != null ? getLoginUser(session).getAdvertiserId() : null;
        Integer agentId = getLoginUser(session) != null ? getLoginUser(session).getAgentId() : null;

        // 广告主权限设置广告主id
        if (null != advId) {
            advertiserId = advId;
        }

        // 代理商权限
        if (null != agentId) {
            // 参数验证
            if (null == advertiserId) {
                throw new ParamValidateException(Result.ERR_MISS_PARAMETER, "advertiserId", "Result.err.miss.parameter");
            }
            if (advertiserId <= 0) {
                throw new ParamValidateException(Result.ERR_INVALID_PARAMETER, "advertiserId",
                    "Result.err.invalid.parameter");
            }
            // 权限验证
            if (null != agentId && !customerService.agentExistAdvertiser(agentId, advertiserId)) {
                // TODO 权限验证
                throw new CallBackException(AdvertiserValidateException.ErrorCode.ADVERTISER_NOT_AUTH.getValue(),
                    AdvertiserValidateException.ErrorCode.ADVERTISER_NOT_AUTH.getDesc());
            }
        }

        return RestfulJsonModelAndView
            .buildJsonModelAndView(platformService.getPlatformListModelByAdvertiserId(advertiserId));
    }
    /**
     * 列表接口id，name
     * @param campaignId
     * @return
     */
    @RequestMapping(value = "promotionPlanStruct", method = {RequestMethod.GET})
    public ModelAndView promotionPlanList(@RequestParam int campaignId) throws ParamValidateException {
        if(0 >= campaignId) {
            throw new ParamValidateException(Result.ERR_MISS_PARAMETER,"campaignId", "Result.err.miss.parameter");
        }
        List<MenuVo> menuVoList = promotionPlanService.promotionPlanList(campaignId);
        return RestfulJsonModelAndView.buildJsonModelAndView(menuVoList);
    }

}
